package com.shujia.sql

import org.apache.flink.table.api.{EnvironmentSettings, TableEnvironment}

object Demo4Print {
  def main(args: Array[String]): Unit = {
    val setting: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .inStreamingMode()
      .build()

    val table: TableEnvironment = TableEnvironment.create(setting)

    /**
      * 创建一个source表
      *
      */

    table.executeSql(
      """
        |CREATE TABLE words (
        |  word STRING
        |) WITH (
        |  'connector' = 'kafka',
        |  'topic' = 'words',
        |  'properties.bootstrap.servers' = 'master:9092',
        |  'properties.group.id' = 'testGroup',
        |  'scan.startup.mode' = 'earliest-offset',
        |  'format' = 'csv',
        |  'csv.field-delimiter'='\t',
        |  'csv.ignore-parse-errors' = 'true'
        |)
      """.stripMargin)

    /**
      * print sink表
      * 用于查看原表数据的sink
      * 一般用于测试
      *
      * LIKE语句，基于一个表的字段创建新的表
      *
      */

    table.executeSql(
      """
        |CREATE TABLE print_table
        |WITH ('connector' = 'print')
        |LIKE words (EXCLUDING ALL)
        |
      """.stripMargin)


    table.executeSql(
      """
        |CREATE TABLE print_count(
        | word STRING,
        | c BIGINT
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)



    //将查询结果插入print表中可以在控制台查看结果
    table.executeSql(
      """
        |insert into print_count
        |select word,count(1) as c from words
        |group by word
        |
      """.stripMargin)


  }

}
